package logic

import (
	"context"
	"fmt"
	"shell/models/mysql"

	"shell/houserpc/houserpc"
	"shell/houserpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PreAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPreAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PreAddLogic {
	return &PreAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PreAddLogic) PreAdd(in *houserpc.PreAddRequest) (*houserpc.PreAddResponse, error) {
	// todo: add your logic here and delete this line
	var pre mysql.Pre
	mysql.DB.Debug().Where("pre_time=?", in.PreTime).Find(&pre)

	if pre.ID != 0 {
		return nil, fmt.Errorf("改时间段已被预约")
	}
	pre = mysql.Pre{
		UserId:  in.UserId,
		HouseId: in.HouseId,
		PreTime: in.PreTime,
		Status:  "预约中",
	}
	mysql.DB.Debug().Create(&pre)
	return &houserpc.PreAddResponse{
		PreId: int64(pre.ID),
	}, nil
}
